﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Text;
using System.Reflection;
using System.Resources;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;

namespace PhotoBox.PhotoBox_Webpart
{
    [ToolboxItem(false)]
    public partial class PhotoBox_Webpart : System.Web.UI.WebControls.WebParts.WebPart
    {
        /// <summary>
        /// private image library source property
        /// </summary>
         private string listname;
         
        [Personalizable(PersonalizationScope.Shared), WebBrowsable(true),
         WebDisplayName("Library Name"), DefaultValue(""), Category("Settings"), Description("Images Library to will be load")]
         public string listName
         {
             get { return listname; }
             set { listname = value; }
         }


        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeControl();
        }
    

        protected void Page_Load(object sender, EventArgs e)
        {
            this.MessageLabel1.Visible = false;

            try
            {
                if (string.IsNullOrEmpty(listname))
                    throw new Exception("The image library name is empty");

                string listUrl = SPContext.Current.Web.Url + "/_vti_bin/ListData.svc/" + this.listname;
                SPList imagesLibrary = SPContext.Current.Web.Lists[listname];

                if (!imagesLibrary.Fields.ContainsField("OpenTo"))
                    throw new Exception("The OpenTo column not created into the list");

                string titleCultureName = imagesLibrary.Fields.GetFieldByInternalName("Title").Title;
                
                StringBuilder JavaScriptDiagramStringBuilder = new StringBuilder();                
                JavaScriptDiagramStringBuilder.Append("<link rel=\"stylesheet\" href=\"PhotoBox/twitter-bootstrap/2.2.1/css/bootstrap-combined.min.css\">");
                JavaScriptDiagramStringBuilder.Append("\n");  
                JavaScriptDiagramStringBuilder.Append("<link rel=\"stylesheet\" href=\"PhotoBox/styles.css\">");
                JavaScriptDiagramStringBuilder.Append("\n");  
                JavaScriptDiagramStringBuilder.Append("<link rel=\"stylesheet\" href=\"PhotoBox/photobox/photobox.css\">");
                JavaScriptDiagramStringBuilder.Append("\n");  
                JavaScriptDiagramStringBuilder.Append("<script src=\"PhotoBox/jquery/1.8.3/jquery.min.js\"></script>");
                JavaScriptDiagramStringBuilder.Append("\n");  
                JavaScriptDiagramStringBuilder.Append("<script src=\"PhotoBox/photobox/photobox.js\"></script>");
                JavaScriptDiagramStringBuilder.Append("\n");              
                JavaScriptDiagramStringBuilder.Append("<ul id='gallery' class='photobox'></ul>");
                JavaScriptDiagramStringBuilder.Append("\n");
                JavaScriptDiagramStringBuilder.Append(new scriptClass(titleCultureName).GetScript(listUrl));            
                JavaScriptLiteral1.Text = JavaScriptDiagramStringBuilder.ToString();
            }


            catch (Exception ex)
            {
                MessageLabel1.Visible = true;
                MessageLabel1.Text = ex.Message;
            }


        }

       

        /// <summary>
        /// Method to create the web control
        /// </summary>
        protected override void CreateChildControls()
        {
            

            /*
            Control control = Page.LoadControl(_ascxPath);

            Literal JavaScriptLiteral = (Literal)control.FindControl("JavaScriptLiteral1");
            Label MessageLabel = (Label)control.FindControl("MessageLabel1");
          
            Literal JavaScriptLiteral = (Literal)this.FindControl("JavaScriptLiteral1");
            Label MessageLabel = (Label)this.FindControl("MessageLabel1");
              */                      

            //Controls.Add(control);
        }



        

    }



}
